package org.springblade.common.handle;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpMessageHandler;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springblade.modules.admin.entity.UserWeixin;
import org.springblade.modules.admin.service.UserWeixinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Slf4j
@Component
public class UnsubscribeHandler implements WxMpMessageHandler {

	@Autowired
	private UserWeixinService userWeixinService;

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
									Map<String, Object> context, WxMpService wxMpService,
									WxSessionManager sessionManager) {
        String openId = wxMessage.getFromUser();
        log.info("取消关注用户 OPENID: " + openId);
        // 更新数据库状态为取消关注状态
		userWeixinService.update(Wrappers.<UserWeixin>lambdaUpdate().set(UserWeixin::getSubscribe,0).eq(UserWeixin::getOpenid,openId));

        return null;
    }

}
